package common.hua.com.flow;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hua.common.http.HttpUtil;

import common.hua.com.myapplication.R;


/**
 * Created by hxl on 2016/9/18.
 */
public class AddFlowActivity extends Activity implements View.OnClickListener {

    private Button doBtn = null;
    private EditText numEdit, sizeEdit;

    ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flow_layout);
        this.initViews();
    }

    private void initViews() {
        this.doBtn = (Button) this.findViewById(R.id.do_btn);
        this.numEdit = (EditText) this.findViewById(R.id.phone_num);
        this.sizeEdit = (EditText) this.findViewById(R.id.flow_size);
        this.doBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (this.progressDialog != null && this.progressDialog.isShowing()) {
            this.progressDialog.dismiss();
        } else {
            this.showProgressDialog();
            addFlow1();
        }
    }

    private void addFlow1() {
        final RequestBean requestBean = getRequestBean();
        requestBean.setReqBody(getRequestBody1());
        String url = "https://clientaccess.10086.cn/biz-V2.2/LN/telLogin/getIdentCode";
        HttpUtil.getInstance().postRequest(url, JSON.toJSONString(requestBean), new HttpUtil.HttpUtilCallBack() {
            @Override
            public void onFailure(int code) {
                Toast.makeText(getApplicationContext(), "网络请求失败", Toast.LENGTH_LONG).show();
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onResponse(String responseBody) {
                ResponseBean responseBean = JSON.parseObject(responseBody, ResponseBean.class);
                if (responseBean != null && responseBean.getRetCode() != null && responseBean.getRetCode().equals("000000")) {
                    ResponseBody responseBody1 = JSON.parseObject(responseBean.getRspBody(), ResponseBody.class);
                    String[] identcodes = responseBody1.getIdentCode().split("==========");
                    addFlow2(identcodes[0]);
                } else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_LONG).show();
                    if (progressDialog != null) {
                        progressDialog.dismiss();
                    }
                }
            }
        });
    }

    private void addFlow2(String identCode) {
        String url = "https://clientaccess.10086.cn/biz-V2.2/BN/bsnsTransactionServices/getBsnsTransactions";
        final RequestBean requestBean = getRequestBean();
        requestBean.setReqBody(getRequestBody2(identCode));
        HttpUtil.getInstance().postRequest(url, JSON.toJSONString(requestBean), new HttpUtil.HttpUtilCallBack() {
            @Override
            public void onFailure(int code) {
                Toast.makeText(getApplicationContext(), "网络请求失败", Toast.LENGTH_LONG).show();
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onResponse(String responseBody) {
                ResponseBean responseBean = JSON.parseObject(responseBody, ResponseBean.class);
                String toastMessage=null;
                if (responseBean != null && responseBean.getRetCode() != null && responseBean.getRetCode().equals("213025")) {
                    toastMessage="成功";
                }else{
                    toastMessage="失败";
                }
                Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show();
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
            }
        });
    }

    private RequestBean getRequestBean() {
        RequestBean requestBean = new RequestBean();
        requestBean.setCv("3.0.0");
        requestBean.setEn("3");
        requestBean.setEn("vivoY18L");
        requestBean.setSp("720x1280");
        requestBean.setSt("1");
        return requestBean;
    }

    private String getRequestBody1() {
        RequestBody requestBody = new RequestBody();
        requestBody.setCellNum(numEdit.getText().toString());
        return JSON.toJSONString(requestBody);
    }

    private String getRequestBody2(String id) {
        RequestBody requestBody = new RequestBody();
        requestBody.setCellNum(numEdit.getText().toString());
        requestBody.setBunessCode(sizeEdit.getText().toString());
        requestBody.setIdentCode(id);
        requestBody.setBunessType("01");
        requestBody.setBunessName("4G流量");
        requestBody.setOprCode("01");
        requestBody.setEffectMode("01");
        return JSON.toJSONString(requestBody);
    }


    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setCancelable(false);
            progressDialog.setMessage("加载中...");
        }
        progressDialog.show();
    }


}
